Imports Gen = System.Collections.Generic
Imports Ref = System.Reflection
Imports Sys = System

Namespace Enhancements.Core.Logging
    ''' <summary>Represents a collection of level mappings.</summary>
    <DebuggerStepThrough()> _
    Public NotInheritable Class LevelMappingCollection

        Private __Collection As Gen.List(Of LevelMapping) = New Gen.List(Of LevelMapping)

        Friend Sub New(ByVal _levelMappingGlobal As LevelMappingGlobal)
            ___Assert.___IsDisposedNot("LevelMappingCollection[instance]", Me.___DisposeCalled, True)
            ___Assert.___IsNullNot("_levelMappingGlobal", _levelMappingGlobal, True)

            __Collection.Add(_levelMappingGlobal)
        End Sub

        ''' <summary>Adds a level mapping to the collection.</summary>
        ''' <param name="_levelMapping">LevelMapping to add.</param>
        ''' <exception cref="IsDisposedException">The instance has been disposed.</exception>
        ''' <exception cref="IsNullException">_levelMapping is null.</exception>
        ''' <exception cref="IsEqualException">The level mapping is already in the collection.</exception>
        Public Sub Add(ByVal _levelMapping As LevelMappingAssembly)
            ___Assert.___IsDisposedNot("LevelMappingCollection[instance]", Me.___DisposeCalled, True)
            ___Assert.___IsNullNot("_levelMapping", _levelMapping, True)

            For Each _collectionLevelMapping As LevelMapping In __Collection
                If ___Assert.___IsOfType("_collectionLevelMapping", _collectionLevelMapping, GetType(LevelMappingAssembly), False) Then
                    ___Assert.___IsEqualNot("_levelMapping.Assembly.FullName", _levelMapping.Assembly.FullName, "CType(_collectionLevelMapping, LevelMappingAssembly).Assembly.FullName", CType(_collectionLevelMapping, LevelMappingAssembly).Assembly.FullName, True)
                End If
            Next _collectionLevelMapping

            __Collection.Add(_levelMapping)
        End Sub

        ''' <summary>Adds a level mapping to the collection.</summary>
        ''' <param name="_levelMapping">LevelMapping to add.</param>
        ''' <exception cref="IsDisposedException">The instance has been disposed.</exception>
        ''' <exception cref="IsNullException">_levelMapping is null.</exception>
        ''' <exception cref="IsEqualException">The level mapping is already in the collection.</exception>
        Public Sub Add(ByVal _levelMapping As LevelMappingType)
            ___Assert.___IsDisposedNot("LevelMappingCollection[instance]", Me.___DisposeCalled, True)
            ___Assert.___IsNullNot("_levelMapping", _levelMapping, True)

            For Each _collectionLevelMapping As LevelMapping In __Collection
                If ___Assert.___IsOfType("_collectionLevelMapping", _collectionLevelMapping, GetType(LevelMappingType), False) Then
                    ___Assert.___IsEqualNot("_levelMapping.Type.FullName", _levelMapping.Type.FullName, "CType(_collectionLevelMapping, LevelMappingType).Type.FullName", CType(_collectionLevelMapping, LevelMappingType).Type.FullName, True)
                End If
            Next _collectionLevelMapping

            __Collection.Add(_levelMapping)
        End Sub

        ''' <summary>Clears the collection, except for the LevelMappingGlobal item.</summary>
        ''' <exception cref="IsDisposedException">The instance has been disposed.</exception>
        Public Sub Clear()
            ___Assert.___IsDisposedNot("LevelMappingCollection[instance]", Me.___DisposeCalled, True)

            For _counter As Sys.Int32 = __Collection.Count - 1 To 0 Step -1
                Dim _collectionLevelMapping As LevelMapping = __Collection(_counter)

                If ___Assert.___IsOfTypeNot("_collectionLevelMapping", _collectionLevelMapping, GetType(LevelMappingGlobal), False) Then
                    __Collection.RemoveAt(_counter)
                End If
            Next _counter
        End Sub

        ''' <summary>Removes a level mapping from the collection.</summary>
        ''' <param name="_levelMapping">LevelMapping to remove.</param>
        ''' <exception cref="IsDisposedException">The instance has been disposed.</exception>
        ''' <exception cref="IsNullException">_levelMapping is null.</exception>
        ''' <exception cref="IsTrueNotException">The level mapping is not in the collection.</exception>
        Public Sub Remove(ByVal _levelMapping As LevelMappingAssembly)
            ___Assert.___IsDisposedNot("LevelMappingCollection[instance]", Me.___DisposeCalled, True)
            ___Assert.___IsNullNot("_levelMapping", _levelMapping, True)
            ___Assert.___IsTrue("__Collection.Contains(_levelMapping)", __Collection.Contains(_levelMapping), True)

            __Collection.Remove(_levelMapping)
        End Sub

        ''' <summary>Removes a level mapping from the collection.</summary>
        ''' <param name="_levelMapping">LevelMapping to remove.</param>
        ''' <exception cref="IsDisposedException">The instance has been disposed.</exception>
        ''' <exception cref="IsNullException">_levelMapping is null.</exception>
        ''' <exception cref="IsTrueNotException">The level mapping is not in the collection.</exception>
        Public Sub Remove(ByVal _levelMapping As LevelMappingType)
            ___Assert.___IsDisposedNot("LevelMappingCollection[instance]", Me.___DisposeCalled, True)
            ___Assert.___IsNullNot("_levelMapping", _levelMapping, True)
            ___Assert.___IsTrue("__Collection.Contains(_levelMapping)", __Collection.Contains(_levelMapping), True)

            __Collection.Remove(_levelMapping)
        End Sub

        ''' <summary>Removes a level mapping from the collection.</summary>
        ''' <param name="_assembly">Assembly's level mapping to remove.</param>
        ''' <exception cref="IsDisposedException">The instance has been disposed.</exception>
        ''' <exception cref="IsNullException">_assembly is null.</exception>
        ''' <exception cref="IsTrueNotException">No level mapping exists in the collection for the specified assembly.</exception>
        Public Sub Remove(ByVal _assembly As Ref.Assembly)
            ___Assert.___IsDisposedNot("LevelMappingCollection[instance]", Me.___DisposeCalled, True)
            ___Assert.___IsNullNot("_assembly", _assembly, True)
            Dim _removeCompleted As Boolean = False

            For Each _collectionLevelMapping As LevelMapping In __Collection
                If ___Assert.___IsOfType("_collectionLevelMapping", _collectionLevelMapping, GetType(LevelMappingAssembly), False) Then
                    If ___Assert.___IsEqual("CType(_collectionLevelMapping, LevelMappingAssembly).Assembly.FullName", CType(_collectionLevelMapping, LevelMappingAssembly).Assembly.FullName, "_assembly.FullName", _assembly.FullName, False) Then

                        __Collection.Remove(_collectionLevelMapping)
                        _removeCompleted = True
                        Exit For

                    End If
                End If
            Next _collectionLevelMapping

            ___Assert.___IsTrue("_removeCompleted", _removeCompleted, True)
        End Sub

        ''' <summary>Removes a level mapping from the collection.</summary>
        ''' <param name="_type">Type's level mapping to remove.</param>
        ''' <exception cref="IsDisposedException">The instance has been disposed.</exception>
        ''' <exception cref="IsNullException">_type is null.</exception>
        ''' <exception cref="IsTrueNotException">No level mapping exists in the collection for the specified type.</exception>
        Public Sub Remove(ByVal _type As Sys.Type)
            ___Assert.___IsDisposedNot("LevelMappingCollection[instance]", Me.___DisposeCalled, True)
            ___Assert.___IsNullNot("_type", _type, True)
            Dim _removeCompleted As Boolean = False

            For Each _collectionLevelMapping As LevelMapping In __Collection
                If ___Assert.___IsOfType("_collectionLevelMapping", _collectionLevelMapping, GetType(LevelMappingType), False) Then
                    If ___Assert.___IsEqual("CType(_collectionLevelMapping, LevelMappingType).Type.FullName", CType(_collectionLevelMapping, LevelMappingType).Type.FullName, "_type.FullName", _type.FullName, False) Then

                        __Collection.Remove(_collectionLevelMapping)
                        _removeCompleted = True
                        Exit For

                    End If
                End If
            Next _collectionLevelMapping

            ___Assert.___IsTrue("_removeCompleted", _removeCompleted, True)
        End Sub

        ''' <summary>Gets a level mapping of the collection.</summary>
        ''' <param name="_assembly">Assembly's level mapping to get.</param>
        ''' <exception cref="IsDisposedException">The instance has been disposed.</exception>
        ''' <exception cref="IsNullException">_assembly is null.</exception>
        ''' <returns>May return null.</returns>
        Default Public ReadOnly Property Item(ByVal _assembly As Ref.Assembly) As LevelMappingAssembly
            Get
                ___Assert.___IsDisposedNot("LevelMappingCollection[instance]", Me.___DisposeCalled, True)
                ___Assert.___IsNullNot("_assembly", _assembly, True)

                Dim _returnValue As LevelMappingAssembly = Nothing

                For Each _collectionLevelMapping As LevelMapping In __Collection
                    If ___Assert.___IsOfType("_collectionLevelMapping", _collectionLevelMapping, GetType(LevelMappingAssembly), False) Then
                        Dim _collectionLevelMappingCasted As LevelMappingAssembly = CType(_collectionLevelMapping, LevelMappingAssembly)
                        If ___Assert.___IsEqual("_collectionLevelMappingCasted.Assembly.FullName", _collectionLevelMappingCasted.Assembly.FullName, "_assembly.FullName", _assembly.FullName, False) Then

                            _returnValue = _collectionLevelMappingCasted
                            Exit For

                        End If
                    End If
                Next _collectionLevelMapping

                Return _returnValue
            End Get
        End Property

        ''' <summary>Gets a level mapping of the collection.</summary>
        ''' <param name="_type">Type's level mapping to get.</param>
        ''' <exception cref="IsDisposedException">The instance has been disposed.</exception>
        ''' <exception cref="IsNullException">_type is null.</exception>
        ''' <returns>May return null.</returns>
        Default Public ReadOnly Property Item(ByVal _type As Sys.Type) As LevelMappingType
            Get
                ___Assert.___IsDisposedNot("LevelMappingCollection[instance]", Me.___DisposeCalled, True)
                ___Assert.___IsNullNot("_type", _type, True)

                Dim _returnValue As LevelMappingType = Nothing

                For Each _collectionLevelMapping As LevelMapping In __Collection
                    If ___Assert.___IsOfType("_collectionLevelMapping", _collectionLevelMapping, GetType(LevelMappingType), False) Then
                        Dim _collectionLevelMappingCasted As LevelMappingType = CType(_collectionLevelMapping, LevelMappingType)
                        If ___Assert.___IsEqual("_collectionLevelMappingCasted.Type.FullName", _collectionLevelMappingCasted.Type.FullName, "_type.FullName", _type.FullName, False) Then

                            _returnValue = _collectionLevelMappingCasted
                            Exit For

                        End If
                    End If
                Next _collectionLevelMapping

                Return _returnValue
            End Get
        End Property

        ''' <summary>Gets the global level mapping of the collection.</summary>
        ''' <exception cref="IsDisposedException">The instance has been disposed.</exception>
        Public ReadOnly Property [Global]() As LevelMappingGlobal
            Get
                ___Assert.___IsDisposedNot("LevelMappingCollection[instance]", Me.___DisposeCalled, True)

                Dim _returnValue As LevelMappingGlobal = Nothing

                For Each _collectionLevelMapping As LevelMapping In __Collection
                    If ___Assert.___IsOfType("_collectionLevelMapping", _collectionLevelMapping, GetType(LevelMappingGlobal), False) Then

                        _returnValue = CType(_collectionLevelMapping, LevelMappingGlobal)
                        Exit For

                    End If
                Next _collectionLevelMapping

                Return _returnValue
            End Get
        End Property

        Private __DisposeCalled As System.Boolean = False
        Friend Property ___DisposeCalled() As System.Boolean
            Get
                Return __DisposeCalled
            End Get
            Private Set(ByVal _value As System.Boolean)
                ___Assert.___IsDisposedNot("LevelMappingCollection[instance]", ___DisposeCalled, True)
                ___Assert.___IsTrue("___DisposeCalled._value", _value, True)

                __DisposeCalled = _value
            End Set
        End Property
        Friend Sub ___Dispose()
            ___Assert.___IsDisposedNot("LevelMappingCollection[instance]", ___DisposeCalled, True)
            ___DisposeCalled = True

            __Collection.Clear()
        End Sub

    End Class
End Namespace
